package kitrobot.com.bed.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import kitrobot.com.bed.Activity.ChangeActivity;
import kitrobot.com.bed.Activity.LoginActivity;
import kitrobot.com.bed.Activity.New_nameActivity;
import kitrobot.com.bed.Activity.TransferActivity;
import kitrobot.com.bed.Activity.UpdatepersonActivity;
import kitrobot.com.bed.Service.SelepoService;
import kitrobot.com.bed.Service.SeleuserService;
import kitrobot.com.wechat_bottom_navigation.R;

/**
 * on 2017/5/8.
 * 类的描述:
 */

public class MineFragment extends Fragment {
    private TextView tv_tel;
    private TextView tv_name;
    private TextView tv_sf;
    private String phone;
    private String type;
    private String name;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mymessage, container, false);
        Button zrqx= (Button) view.findViewById(R.id.bt_pt);
        TextView newname=(TextView) view.findViewById(R.id.tv_edit);
        Button logout= (Button) view.findViewById(R.id.rb_lou);
        SharedPreferences sp = getActivity().getSharedPreferences("loginToken",
                Context.MODE_PRIVATE);
        tv_tel= (TextView) view.findViewById(R.id.tv_tel);
        tv_name= (TextView) view.findViewById(R.id.tv_name);
        tv_sf= (TextView) view.findViewById(R.id.tv_sf);
        final String id = sp.getString("id", "");
        final String token = sp.getString("token", "");
        new Thread(){
            public void run(){
                final String result= SeleuserService.SeleuserByClientPost(id,token);
                if (result!=null){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                //JSON字符串
                                //根据JSON字符串 获得JSON object对象
                                JSONObject jsonObject = new JSONObject(result);
                                //根据key 获取相应的value
                                JSONObject data=jsonObject.getJSONObject("data");
                                phone = data.getString("phone");
                                name = data.getString("name");
                                type = data.getString("type");
                                tv_tel.setText(phone);
                                tv_name.setText(name);
                                SharedPreferences sp = getActivity().getSharedPreferences("loginToken", 0);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("name",name);
                                editor.commit();
                                String identity=null;
                                if(type.equals("0")){
                                    identity="医疗人员";
                                }
                                else if(type.equals("1")){
                                    identity="普通用户";
                                }
                                tv_sf.setText(identity);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    });
                }
                else{
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity().getApplicationContext(),"结果"+result,Toast.LENGTH_LONG).show();
                        }
                    });
                }
            };

        }.start();



        zrqx.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), ChangeActivity.class);
                startActivity(intent);

            }
        });
        newname.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), New_nameActivity.class);
                startActivity(intent);

            }
        });
        logout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                SharedPreferences sp = getActivity().getSharedPreferences("loginToken", 0);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("id",null);
                editor.putString("name",null);
                editor.putString("phone","");
                editor.putString("token","");
                editor.putString("type","");
                editor.putString("password","");
                editor.commit();
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        return view;
    }
}

